package com.sunop.panticlass.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.sunop.panticlass.model.ClassPO;
import com.sunop.panticlass.service.ClassService;
import com.sunop.panticommon.entity.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping
@AllArgsConstructor
public class ClassController {

    private final ClassService classService;

    @PostMapping("/task/commit")
    public Result commitTask(int messageId) {
        return classService.commitTask(messageId);
    }

    /*
     * 获取班级消息
     */
    @GetMapping("/class/messages")
    public Result getMessage() {
        return classService.getMessage();
    }

    // 创建班级队列
    @PostMapping("/create/{classId}")
    @SaCheckRole("teacher")
    public Result<String> setupClassQueue(@PathVariable String classId) {
        if (classService.existsClassQueue(classId)) {
            return Result.error("Queue already exists");
        }
        classService.createClassQueue(classId);
        return Result.success(null,"Class queue created");
    }

    @PostMapping("/send/message")
    @SaCheckRole("teacher")
    public Result<String> sendMessage(@RequestBody ClassPO classPo,
                                      @RequestParam(required = false) List<Integer> problemIds) {
        //发送班级消息（自动创建队列）
        classService.sendClassMessage(classPo, problemIds);
        return Result.success(null,"Message sent to class");
    }

    @DeleteMapping("/class/{classId}")
    @SaCheckRole("teacher")
    public Result<String> cleanupClassQueue(@PathVariable String classId) {
        if (!classService.existsClassQueue(classId)) {
            return Result.error("Queue does not exist");
        }
        classService.removeClassQueue(classId);
        return Result.success(null,"Class queue removed");
    }
}
